<?php

namespace app\common\config\logic;


use app\common\BaseLogic;
use app\common\config\model\ConfigDictionary;

class CommonPickerLogic extends BaseLogic
{
    public function getPicker($type) {
        $cacheName = "common::picker:{$type}";
        $data = $this->redis->get($cacheName);

        if (empty($data)) {
            $model = new ConfigDictionary();
            $row = $model->where('field', $type)->find();
            if (empty($row)) return [];

            $this->redis->set($cacheName, $row->getOrigin('value'));
            $result = $row->value;
        } else {
            $result = json_decode($data, 1);
        }

        return $result;
    }

    public function getPickerItem($type, $id){
        $target = [];

        $items = $this->getPicker($type);
        foreach ($items as $item) {
            if ($item['id'] == $id) {
                $target = $item;
            }
        }

        return $target;
    }

    public function resetCache($type) {
        $cacheName = "common::picker:{$type}";
        $this->redis->del($cacheName);
    }
}